

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>cogentviewer.models.meta &mdash; Cogent Viewer 0.1 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/rtd.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '0.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="Cogent Viewer 0.1 documentation" href="../../../index.html" />
    <link rel="up" title="Module code" href="../../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../index.html">Cogent Viewer 0.1 documentation</a> &raquo;</li>
          <li><a href="../../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for cogentviewer.models.meta</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">This metaclass stops the need for the Base function being initiated by all</span>
<span class="sd">models, That saves the poor things getting confused with scope.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declarative_base</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.orm</span> <span class="kn">import</span> <span class="n">scoped_session</span><span class="p">,</span> <span class="n">sessionmaker</span>

<span class="kn">import</span> <span class="nn">logging</span>
<span class="n">log</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span>


<span class="c">#Functions provided by from meta import *</span>
<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;Base&#39;</span><span class="p">,</span> <span class="s">&#39;Session&#39;</span><span class="p">]</span>


<span class="c">#PYRAMID IMPORTS (COMMENT THESE FOR NON PYRAMID OPERATION)</span>
<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">zope.sqlalchemy</span> <span class="kn">import</span> <span class="n">ZopeTransactionExtension</span>
    <span class="n">Session</span> <span class="o">=</span> <span class="n">scoped_session</span><span class="p">(</span><span class="n">sessionmaker</span><span class="p">(</span><span class="n">extension</span><span class="o">=</span><span class="n">ZopeTransactionExtension</span><span class="p">()))</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="c">#STANDARD IMPORTS</span>
    <span class="c"># SQLAlchemy session manager. Updated by model.init_model()</span>
    <span class="n">Session</span> <span class="o">=</span> <span class="n">scoped_session</span><span class="p">(</span><span class="n">sessionmaker</span><span class="p">())</span>

<span class="c"># SQLAlchemy session manager. Updated by model.init_model()</span>
<span class="c">#Session = scoped_session(sessionmaker())</span>

<span class="kn">import</span> <span class="nn">sqlalchemy</span>

<span class="kn">import</span> <span class="nn">dateutil.parser</span>

<span class="kn">import</span> <span class="nn">json</span>

<span class="c"># The declarative Base</span>
<span class="n">Base</span> <span class="o">=</span> <span class="n">declarative_base</span><span class="p">()</span>



<div class="viewcode-block" id="InnoDBMix"><a class="viewcode-back" href="../../../models.html#cogentviewer.models.meta.InnoDBMix">[docs]</a><span class="k">class</span> <span class="nc">InnoDBMix</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Mixin Base class for all models used in the viewer.</span>

<span class="sd">    Primarily this makes sure that all new database tables are created</span>
<span class="sd">    using the InnoDB engine.</span>

<span class="sd">    Addtionally this class defines standard functionality that should</span>
<span class="sd">    be included in all modules.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">__table_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;mysql_engine&#39;</span><span class="p">:</span> <span class="s">&#39;InnoDB&#39;</span><span class="p">,</span>
                      <span class="s">&#39;mysql_charset&#39;</span><span class="p">:</span><span class="s">&#39;utf8&#39;</span><span class="p">}</span>


<div class="viewcode-block" id="InnoDBMix.update"><a class="viewcode-back" href="../../../models.html#cogentviewer.models.meta.InnoDBMix.update">[docs]</a>    <span class="k">def</span> <span class="nf">update</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Update an object using keyword arguments</span>

<span class="sd">        :param kwargs: List of keyword arguments to add to this object</span>

<span class="sd">        .. note::</span>

<span class="sd">            Unlike the keyword based __init__ function provided by Base. This</span>
<span class="sd">            has no sanity checking.  Items that have no database column can be</span>
<span class="sd">            added to the database, however they will not be saved when the data</span>
<span class="sd">            is committed.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="InnoDBMix.toDict"><a class="viewcode-back" href="../../../models.html#cogentviewer.models.meta.InnoDBMix.toDict">[docs]</a>    <span class="k">def</span> <span class="nf">toDict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Helper Method to convert an object to a dictionary.</span>

<span class="sd">        This will return a dictionary object, representing this object.</span>

<span class="sd">        .. note::  As this is intended to simplify conversion to and from JSON,</span>
<span class="sd">                   datetimes are converted using .isoformat()</span>

<span class="sd">        :return:: A dictionary of {__table__:&lt;tablename&gt; .. (key,value)* pairs}</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c">#Appending a table to the dictionary could help us be a little cleverer</span>
        <span class="c">#when unpacking objects</span>
        <span class="n">out</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;__table__&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">__tablename__</span><span class="p">}</span>

        <span class="c">#Iterate through each column in our table</span>
        <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__table__</span><span class="o">.</span><span class="n">columns</span><span class="p">:</span>
            <span class="c">#Get the value from the namespace (warning, may not work if there is</span>
            <span class="c">#any trickery with column names and names in the python object) Such</span>
            <span class="c">#as in the case of reading.type (typeId) what feckin ejit did that.</span>
            <span class="c">#For the moment we will use a try / except hack To get around it.</span>
            <span class="c">#print col.name</span>

            <span class="k">try</span><span class="p">:</span>
                <span class="n">value</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">col</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
                <span class="n">log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s">&quot;Conversion Error {0}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>

            <span class="c">#Conversion code for datetime</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">col</span><span class="o">.</span><span class="n">type</span><span class="p">,</span> <span class="n">sqlalchemy</span><span class="o">.</span><span class="n">DateTime</span><span class="p">)</span> <span class="ow">and</span> <span class="n">value</span><span class="p">:</span>
                <span class="n">value</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span>
            <span class="c">#Append to the dictionary</span>
            <span class="n">out</span><span class="p">[</span><span class="n">col</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>

        <span class="k">return</span> <span class="n">out</span>
</div>
<div class="viewcode-block" id="InnoDBMix.fromJSON"><a class="viewcode-back" href="../../../models.html#cogentviewer.models.meta.InnoDBMix.fromJSON">[docs]</a>    <span class="k">def</span> <span class="nf">fromJSON</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">jsonDict</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Update the object using a JSON string</span>

<span class="sd">        :var jsonDict:: Either a JSON string (from json.dumps) or dictionary</span>
<span class="sd">        containing key,value pairs (from asDict())</span>

<span class="sd">        .. note::</span>

<span class="sd">            This Is a candidate for moving to a class or staticmethod,</span>
<span class="sd">            But inheritance makes it a little bit of a pain</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c">#Check if we have a string or dictonary</span>
        
        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;{0} Demung {1}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;=_&quot;</span><span class="o">*</span><span class="mi">25</span><span class="p">,</span><span class="n">jsonDict</span><span class="p">))</span>

        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">jsonDict</span><span class="p">)</span> <span class="o">==</span> <span class="nb">str</span><span class="p">:</span>
            <span class="n">jsonDict</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">jsonDict</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">jsonDict</span><span class="p">)</span> <span class="o">==</span> <span class="nb">list</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s">&quot;WARNING LIST SUPPLIED {0}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">jsonDict</span><span class="p">))</span>
            <span class="n">jsonDict</span> <span class="o">=</span> <span class="n">jsonDict</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="c">#log.info(type(self))</span>
        <span class="c">#log.info(self.__table__)</span>
        <span class="c">#For each column in the table</span>
        <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__table__</span><span class="o">.</span><span class="n">columns</span><span class="p">:</span>
            <span class="c">#Check to see if the item exists in our dictionary</span>
            <span class="n">newValue</span> <span class="o">=</span> <span class="n">jsonDict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">col</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
            
            <span class="c">#Fix missing values</span>
            <span class="c">#if not self.__table__</span>
            <span class="k">if</span> <span class="n">col</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s">&quot;locationId&quot;</span><span class="p">:</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">col</span><span class="o">.</span><span class="n">name</span><span class="p">,</span><span class="n">newValue</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">newValue</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                <span class="k">pass</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c">#Convert if it is a datetime object</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">col</span><span class="o">.</span><span class="n">type</span><span class="p">,</span> <span class="n">sqlalchemy</span><span class="o">.</span><span class="n">DateTime</span><span class="p">)</span> <span class="ow">and</span> <span class="n">newValue</span><span class="p">:</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;{0} CASTING DATE {0}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;-=&quot;</span><span class="o">*</span><span class="mi">15</span><span class="p">))</span>
                    <span class="k">print</span> <span class="n">newValue</span>
                    <span class="n">newValue</span> <span class="o">=</span> <span class="n">dateutil</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">newValue</span><span class="p">,</span><span class="n">ignoretz</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;New Time {0}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">newValue</span><span class="p">))</span>
                <span class="c">#And set our variable</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">col</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">newValue</span><span class="p">)</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../index.html">Cogent Viewer 0.1 documentation</a> &raquo;</li>
          <li><a href="../../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2011, Daniel Goldsmith.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>